package com.wn.controller;

import com.wn.entity.Article;
import com.wn.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//@Controller
//@ResponseBody
@RestController // 相当于@Controller + @ResponseBody
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
     * 请求方式：get请求，处理查询
     * 请求路径：http://localhost:8080/article/100
     *
     * @PathVariable
     * 1、获取rest请求中的路径参数值
     * 2、注解参数对应占位符
     *   @GetMapping("/{id}") 中的id对应 @PathVariable("id") 这里的id
     */
    @GetMapping("/{id}")
    public Article findById(@PathVariable("id") Integer id) {
        Article article = articleService.findById(id);
        return article;
    }

    // 处理查询请求
    @GetMapping
    public List<Article> findAll() {
        List<Article> list = articleService.findAll();
        return list;
    }

    // 处理添加的请求
    @PostMapping
    public String save(@RequestBody Article article) {
        articleService.save(article);
        return "ok";
    }

    // 处理修改的请求
    @PutMapping
    public String update(@RequestBody Article article) {
        articleService.update(article);
        return "ok";
    }

    // 处理delete请求
    @DeleteMapping
    public String delete(Integer id) {
        articleService.delete(id);
        return "ok";
    }


    // Caused by: java.lang.IllegalStateException: Ambiguous mapping.
    // http://localhost:8080/article/test?id=100
    // http://localhost:8080/article/test?id=100&title=xxx
    @GetMapping("/test")
    public Article findById(Article article,int id) {
        System.out.println("article = " + article);
        System.out.println("id = " + id);
        return article;
    }

    @DeleteMapping("/del/batch")
    public String delBatch(@RequestBody List<Integer> ids) {
        articleService.delBatch(ids);
        return "ok";
    }
}


















